package com.butuh.uang.tuh.ang.ui;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.butuh.uang.tuh.ang.R;
import com.butuh.uang.tuh.ang.adapter.DownHistoryAdapter;
import com.butuh.uang.tuh.ang.adapter.FAQAdapter;
import com.butuh.uang.tuh.ang.listener.OnClickZhanKaiListener;
import com.butuh.uang.tuh.ang.model.FaqModel;
import com.butuh.uang.tuh.ang.model.HomeListBean;
import com.butuh.uang.tuh.ang.present.PFAQ;
import com.butuh.uang.tuh.ang.util.DecimalFormatUtil;
import com.butuh.uang.tuh.ang.util.OnClickLogUtils;
import com.lanren.easydialog.DialogViewHolder;
import com.lanren.easydialog.EasyDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.mvp.XActivity;

public class FAQActivity extends XActivity<PFAQ> implements OnClickZhanKaiListener {
    @BindView(R.id.lv_faq)
    ListView mLvfaq;

    FAQAdapter faqAdapter;

    @BindView(R.id.layout_no_net)
    LinearLayout mLayoutNoNet;
    @BindView(R.id.layout_error)
    LinearLayout mLayoutError;
    @BindView(R.id.tv_error_message)
    TextView mTvErrorMessage;

    String yemianName = "FAQ";

    @Override
    public void initData(Bundle savedInstanceState) {
        OnClickLogUtils.onClickLog(this, "come-in-into", "[\\\"FAQ\\\"]", 0);
        faqAdapter = new FAQAdapter(this, this);
        mLvfaq.setAdapter(faqAdapter);
        mDialog.Show();
        getP().getFAQ();

    }

    public void loadSuccess(String message) {
        mDialog.Dismiss();
        if (!TextUtils.isEmpty(message)) {
            mLvfaq.setVisibility(View.VISIBLE);
            mLayoutNoNet.setVisibility(View.GONE);
            mLayoutError.setVisibility(View.GONE);
            String mDecimalFormatStr = DecimalFormatUtil.htmlToText(message);
            List<FaqModel> models = new ArrayList<>();
            String[] mDecimalFormatStrs = mDecimalFormatStr.split("\\.");
            List<String> indexs = new ArrayList<>();
            List<String> names = new ArrayList<>();
            List<String> msgs = new ArrayList<>();
            for (int i = 0; i < mDecimalFormatStrs.length; i++) {
                if (i % 2 == 0) {
                    indexs.add(mDecimalFormatStrs[i] + ".");
                } else {
                    String[] splits = mDecimalFormatStrs[i].split("\\?");
                    names.add(splits[0] + "?");
                    msgs.add(splits[1] + ".");
                }
            }
            for (int i = 0; i < indexs.size(); i++) {
                models.add(new FaqModel(indexs.get(i) + names.get(i), msgs.get(i), false));
            }
            faqAdapter.setData(models);
        } else {
            mLvfaq.setVisibility(View.GONE);
            mLayoutNoNet.setVisibility(View.GONE);
            mLayoutError.setVisibility(View.VISIBLE);
            mTvErrorMessage.setText("Saat ini tidak ada produk");
        }
    }


    public void loadError(int type, String message) {
        mDialog.Dismiss();
        if (type == 1) {
            mLvfaq.setVisibility(View.GONE);
            mLayoutNoNet.setVisibility(View.VISIBLE);
            mLayoutError.setVisibility(View.GONE);
        } else {
            mLvfaq.setVisibility(View.GONE);
            mLayoutNoNet.setVisibility(View.GONE);
            mLayoutError.setVisibility(View.VISIBLE);
            mTvErrorMessage.setText(message);
        }
    }


    @OnClick({R.id.iv_back, R.id.tv_resume})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                OnClickLogUtils.onClickPermute(context, yemianName, "back", 0);
                break;
            case R.id.tv_resume:
                mDialog.Show();
                getP().getFAQ();
                OnClickLogUtils.onClickPermute(context, yemianName, "errorResume", 0);
                break;
        }
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_faq;
    }

    @Override
    public PFAQ newP() {
        return new PFAQ();
    }

    @Override
    public void onZhanKai(int position, boolean status) {
        FaqModel model = faqAdapter.getDataSource().get(position);
        model.setZhanKai(status);
        faqAdapter.updateElement(model, position);
        OnClickLogUtils.onClickPermute(context, yemianName, "listzhankai_" + status, 0);

    }
}
